package org.jeecg.modules.basic.repair.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.PermissionData;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.student.entity.LwgyBaseStudent;
import org.jeecg.modules.basic.demo.student.mapper.LwgyBaseStudentMapper;
import org.jeecg.modules.basic.demo.student.service.ILwgyBaseStudentService;
import org.jeecg.modules.basic.repair.entity.LwgyRepairMaintenance;
import org.jeecg.modules.basic.repair.service.ILwgyRepairMaintenanceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 报修维修
 * @Author: jeecg-boot
 * @Date:   2021-02-28
 * @Version: V1.0
 */
@Api(tags="报修维修")
@RestController
@RequestMapping("/repair/lwgyRepairMaintenance")
@Slf4j
public class LwgyRepairMaintenanceController extends JeecgController<LwgyRepairMaintenance, ILwgyRepairMaintenanceService> {
	@Autowired
	private ILwgyRepairMaintenanceService lwgyRepairMaintenanceService;

	 @Autowired
	 private ISysBaseAPI sysBaseAPI;

	 @Autowired
	 private ILwgyBaseStudentService lwgyBaseStudentService;

	 @Resource
	 private LwgyBaseStudentMapper lwgyBaseStudentMapper;

	 @Resource
	 private LwgyBaseBedMapper lwgyBaseBedMapper;

	 @Value("${jeecg.path.upload}")
	 private String upLoadPath;
	/**
	 * 分页列表查询
	 *
	 * @param lwgyRepairMaintenance
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "报修维修-分页列表查询")
	@ApiOperation(value="报修维修-分页列表查询", notes="报修维修-分页列表查询")
	@GetMapping(value = "/list")
//	@PermissionData(pageComponent = "repair/repairs/LwgyRepairMaintenanceListMyService")
	public Result<?> queryPageList(LwgyRepairMaintenance lwgyRepairMaintenance,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyRepairMaintenance> queryWrapper = QueryGenerator.initQueryWrapper(lwgyRepairMaintenance, req.getParameterMap());

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		if (roles.contains("user_students")) {
			queryWrapper.eq("bx_username",sysUser.getRealname());
		}else if(roles.contains("wxg")){
			queryWrapper.eq("assign_username",sysUser.getId());
		}else if(roles.contains("lz")){
			List<String> classId = lwgyBaseStudentService.getClassIdBySysUser(sysUser);
			if (oConvertUtils.listIsNotEmpty((classId))) {
				queryWrapper.in("student_class", classId);
			} else {
				queryWrapper.eq("student_class", "null");
			}
		}
		Page<LwgyRepairMaintenance> page = new Page<>(pageNo, pageSize);
		IPage<LwgyRepairMaintenance> pageList = lwgyRepairMaintenanceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param lwgyRepairMaintenance
	 * @return
	 */
	@AutoLog(value = "报修维修-添加")
	@ApiOperation(value="报修维修-添加", notes="报修维修-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		LwgyBaseStudent lwgyBaseStudent =lwgyBaseStudentMapper.getStudentByNumber(lwgyRepairMaintenance.getStudentNumber());

		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> roles = sysBaseAPI.getRolesByUsername(sysUser.getUsername());
		if(!roles.contains("admin")){
			LwgyBaseBed lwgyBaseBed = lwgyBaseBedMapper.getBedByStuId(lwgyBaseStudent.getId());
			if(oConvertUtils.isNotEmpty(lwgyBaseBed)){
				lwgyRepairMaintenance.setBuildingId(lwgyBaseBed.getBuildingId());
				lwgyRepairMaintenance.setUnitId(lwgyBaseBed.getUnitId());
				lwgyRepairMaintenance.setLayerId(lwgyBaseBed.getLayerId());
				lwgyRepairMaintenance.setRoomId(lwgyBaseBed.getRoomId());
			}

		}

		lwgyRepairMaintenance.setBxUsername(sysUser.getRealname());
		lwgyRepairMaintenance.setBxDatetime(new Date());//设置报修时间
		lwgyRepairMaintenanceService.save(lwgyRepairMaintenance);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lwgyRepairMaintenance
	 * @return
	 */
	@AutoLog(value = "报修维修-编辑")
	@ApiOperation(value="报修维修-编辑", notes="报修维修-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		lwgyRepairMaintenanceService.updateById(lwgyRepairMaintenance);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报修维修-通过id删除")
	@ApiOperation(value="报修维修-通过id删除", notes="报修维修-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyRepairMaintenanceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报修维修-批量删除")
	@ApiOperation(value="报修维修-批量删除", notes="报修维修-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyRepairMaintenanceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报修维修-通过id查询")
	@ApiOperation(value="报修维修-通过id查询", notes="报修维修-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyRepairMaintenance lwgyRepairMaintenance = lwgyRepairMaintenanceService.getById(id);
		if(lwgyRepairMaintenance==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyRepairMaintenance);
	}

  /*  *//**
    * 导出excel
    *
    * @param request
    * @param lwgyRepairMaintenance
    *//*
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyRepairMaintenance lwgyRepairMaintenance) {
		return super.exportXls(request, lwgyRepairMaintenance, LwgyRepairMaintenance.class, "报修维修");
    }*/
	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param lwgyRepairMaintenance
	  */
	 //@RequiresRoles({"admin"})
	 //@RequiresPermissions("system:user:export")
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(LwgyRepairMaintenance lwgyRepairMaintenance, HttpServletRequest request) {
		 // Step.1 组装查询条件
		 QueryWrapper<LwgyRepairMaintenance> queryWrapper = QueryGenerator.initQueryWrapper(lwgyRepairMaintenance, request.getParameterMap());
		 //Step.2 AutoPoi 导出Excel
		 ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
		 //update-begin--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据--------------------
		 String selections = request.getParameter("selections");
		 if(!oConvertUtils.isEmpty(selections)){
			 queryWrapper.in("id",selections.split(","));
		 }
		 //update-end--Author:kangxiaolin  Date:20180825 for：[03]用户导出，如果选择数据则只导出相关数据----------------------
		 List<LwgyRepairMaintenance> pageList = lwgyRepairMaintenanceService.list(queryWrapper);

		 //导出文件名称
		 mv.addObject(NormalExcelConstants.FILE_NAME, "用户列表");
		 mv.addObject(NormalExcelConstants.CLASS, LwgyRepairMaintenance.class);
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 ExportParams exportParams = new ExportParams("数据",  "导出信息");
		 exportParams.setImageBasePath(upLoadPath);
		 mv.addObject(NormalExcelConstants.PARAMS, exportParams);
		 mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
		 return mv;
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyRepairMaintenance.class);
    }

	 /**
	  * 已修复状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审核状态")
	 @ApiOperation(value = "审核", notes = "审核")
	 @PutMapping(value = "/changeBxStateUrl")
	 public Result<?>changeBxState(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		 System.out.println(lwgyRepairMaintenance);
		 lwgyRepairMaintenance.getId();
         lwgyRepairMaintenance.setState("1");//审核状态  已修复
         lwgyRepairMaintenance.setBxstate("1");//修复状态  已修复
         lwgyRepairMaintenance.setRepairDatetime(new Date());//修复时间
         lwgyRepairMaintenanceService.updateById(lwgyRepairMaintenance);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 待修补状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审核状态")
	 @ApiOperation(value = "审核", notes = "审核")
	 @PutMapping(value = "/changeBxStateUrl1")
	 public Result<?>changeBxState1(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		 System.out.println(lwgyRepairMaintenance);
		 lwgyRepairMaintenance.getId();
		 lwgyRepairMaintenance.setState("0");//修复状态  未修复
		 lwgyRepairMaintenance.setBxstate("2");//审核状态 待补修
		 lwgyRepairMaintenanceService.changeBxState(lwgyRepairMaintenance);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 被驳回状态
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "审核状态")
	 @ApiOperation(value = "审核", notes = "审核")
	 @PutMapping(value = "/changeBxStateUrl2")
	 public Result<?>changeBxState2(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		 System.out.println(lwgyRepairMaintenance);
		 lwgyRepairMaintenance.getId();
		 lwgyRepairMaintenance.setState("4");//修复状态  被驳回
		 lwgyRepairMaintenance.setBxstate("3");//审核状态  被驳回
		 lwgyRepairMaintenanceService.updateById(lwgyRepairMaintenance);
		 return Result.OK("保存成功!");
	 }
	 /**
	  * 评价
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "评价")
	 @ApiOperation(value = "评价", notes = "评价")
	 @PutMapping(value = "/changeVoteUrl")
	 public Result<?>changeVote(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		 System.out.println("lwgyRepairMaintenance = " + lwgyRepairMaintenance);
		 lwgyRepairMaintenance.getId();
		 lwgyRepairMaintenance.setVote(lwgyRepairMaintenance.getVote());
		 lwgyRepairMaintenance.setVoteRemark(lwgyRepairMaintenance.getVoteRemark());
		 lwgyRepairMaintenanceService.changeVote(lwgyRepairMaintenance);
		 return Result.OK("保存成功!");
	 }

	 /**
	  * 指派人员
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "指派")
	 @ApiOperation(value = "指派", notes = "指派")
	 @PutMapping(value = "/changeAssign")
	 public Result<?>changeAssign(@RequestBody LwgyRepairMaintenance lwgyRepairMaintenance) {
		 lwgyRepairMaintenance.getId();
		 lwgyRepairMaintenance.setState("2");//修复状态 修理中
		 lwgyRepairMaintenance.setAssignDatetime(new Date());//分配时间
		 lwgyRepairMaintenanceService.updateById(lwgyRepairMaintenance);
		 return Result.OK("指派人员成功!");
	 }

	 /**
	  *  批量完成修复
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "报修维修-批量完成修复")
	 @ApiOperation(value="报修维修-批量修复", notes="报修维修-批量修复")
	 @PutMapping(value = "/batchState")
	 public Result<?> batchState(@RequestBody List<LwgyRepairMaintenance>lwgyRepairMaintenances) {
		 for (LwgyRepairMaintenance lwgyRepairMaintenance : lwgyRepairMaintenances) {
			 lwgyRepairMaintenance.setRepairDatetime(new Date());
		 }
		 lwgyRepairMaintenanceService.updateBatchById(lwgyRepairMaintenances);
		 return Result.OK("批量修复成功!");
	 }

	 /**
	  *  批量待补修
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "报修维修-批量待补修")
	 @ApiOperation(value="报修维修-批量待补修", notes="报修维修-批量待补修")
	 @PutMapping(value = "/batchBxState")
	 public Result<?> batchBxState(@RequestBody List<LwgyRepairMaintenance>lwgyRepairMaintenances) {
		 lwgyRepairMaintenanceService.updateBatchById(lwgyRepairMaintenances);
		 return Result.OK("批量审核成功!");
	 }
	 /**
	  *  批量指派
	  *
	  * @param
	  * @return
	  */
	 @AutoLog(value = "报修维修-批量指派")
	 @ApiOperation(value="报修维修-批量指派", notes="报修维修-批量指派")
	 @PutMapping(value = "/batchAssign")
	 public Result<?>batchAssign(@RequestBody List<LwgyRepairMaintenance>lwgyRepairMaintenances) {
		 for (LwgyRepairMaintenance lwgyRepairMaintenance : lwgyRepairMaintenances) {
			 lwgyRepairMaintenance.setAssignDatetime(new Date());
		 }
		 lwgyRepairMaintenanceService.updateBatchById(lwgyRepairMaintenances);
		 return Result.OK("批量指派成功!");
	 }

}
